package bundle2name

import com.alibaba.fastjson.JSON
import com.alibaba.fastjson.JSONObject
import io.vertx.core.Vertx
import io.vertx.kotlin.core.VertxOptions
import io.vertx.kotlin.core.http.HttpClientOptions
import org.jsoup.Jsoup
import org.slf4j.LoggerFactory
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.CountDownLatch
import java.util.concurrent.Semaphore
import java.util.concurrent.TimeUnit
import java.util.concurrent.atomic.AtomicInteger
import kotlin.concurrent.thread

val file = LoggerFactory.getLogger("fileLogger")
val cates = arrayOf(15, 5, 27, 2, 7, 12, 10, 9, 3, 4, 6, 14, 8, 11, 13, 1, 16, 17, 18, 19, 20, 21, 22, 23, 25, 26, 28, 29)
val stops = ConcurrentHashMap<Int, Boolean>()
fun main(args: Array<String>) {
    val vert = Vertx.vertx(VertxOptions(eventLoopPoolSize = 4))
    val client = vert.createHttpClient(HttpClientOptions(defaultHost = "app.mi.com"))
    cates.forEach {
        for (i in 0..70) {
            if(i % 10 == 0){
                TimeUnit.MILLISECONDS.sleep(100)
            }
            client.getNow("/categotyAllListApi?categoryId=${it}&pageSize=50&page=${i}", { res ->
                res.bodyHandler {
                    val jo = JSON.parseObject(it.getString(0, it.length()))
                    val data = jo.getJSONArray("data")
                        data.forEach {
                            val jo = it as JSONObject
                            val bundle = jo.getString("packageName")
                            val name = jo.getString("displayName")
                            file.info("$bundle\t$name")
                        }

                }
            })
        }

    }
}
